package com.dlwx.firefighting.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.dlwx.firefighting.R;
import com.dlwx.firefighting.config.Constant;
import com.dlwx.firefighting.utils.KeyBoardUtils;
import com.dlwx.firefighting.utils.SPUtils;
import com.dlwx.firefighting.utils.ToastUtils;
import com.dlwx.firefighting.utils.db.DbUtil;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 搜索界面
 */
public class SearchActivity extends BaseActivity implements TagFlowLayout.OnTagClickListener, TextWatcher {

    @BindView(R.id.search_back)
    ImageView img_back;
    @BindView(R.id.search_edittext)
    EditText edit_search;
    @BindView(R.id.tv_search_icon)
    TextView tv_search;
    @BindView(R.id.flowlayout_history)
    TagFlowLayout flowlayout_history;
    @BindView(R.id.img_clear)
    ImageView img_clear;

    private String userId;
    private String key="";
    DbUtil util;


    //搜索历史集合
    private List<String> historyLabels = new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        ButterKnife.bind(this);
        util=new DbUtil(SearchActivity.this);
        userId = (String) SPUtils.get(this, Constant.USER_ID, "");
        key=edit_search.getText().toString();

//        edit_search.addTextChangedListener(this);

    }

    @Override
    protected void onResume() {
        super.onResume();
        getHistoryData();
    }

    /**
     * 添加历史数据
     */
    private void getHistoryData() {
        List<String> lists=util.queryData();
        historyLabels.clear();
        if(lists.isEmpty()){
            return;
        }
        Collections.reverse(lists);
        if(lists.size()>16){
            historyLabels.addAll(lists.subList(0,16));
        }else{
            historyLabels.addAll(lists);
        }
        flowlayout_history.setAdapter(new TagAdapter(historyLabels) {
            @Override
            public View getView(FlowLayout parent, int position, Object o) {
                String tip = historyLabels.get(position);
                TextView textView = (TextView) LayoutInflater.from(SearchActivity.this)
                        .inflate(R.layout.item_search_history, flowlayout_history, false);
                textView.setText(tip);
                return textView;
            }
        });
        flowlayout_history.setOnTagClickListener(this);
    }

    @OnClick({R.id.search_back, R.id.img_clear, R.id.tv_search_icon})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.search_back:
                finish();
                break;
            case R.id.img_clear:  //清除按钮
                edit_search.setText("");
                break;
            case R.id.tv_search_icon: //搜索
                KeyBoardUtils.closeKeybord(this);  //关闭软键盘
                key=edit_search.getText().toString();
                if(TextUtils.isEmpty(key)){
                    ToastUtils.toastForShort(SearchActivity.this,"搜索内容不能为空！");
                    return;
                }
                Intent i=new Intent(SearchActivity.this,SearchResultActivity.class);
                i.putExtra("key",key);
                util.insertData(key);
                startActivity(i);
               /* mAdapter.clear();
                lRecyclerViewAdapter.notifyDataSetChanged();
                getDatas();*/
                break;
        }
    }

    /**
     * 搜索历史点击事件
     */
    @Override
    public boolean onTagClick(View view, int position, FlowLayout parent) {
        KeyBoardUtils.closeKeybord(this);  //关闭软键盘
        Intent i=new Intent(SearchActivity.this,SearchResultActivity.class);
        i.putExtra("key",historyLabels.get(position));
        util.insertData(historyLabels.get(position));
        startActivity(i);
        return false;
    }
    /**
     * 搜索框输入监听
     */
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        if (TextUtils.isEmpty(edit_search.getText().toString())) {
            img_clear.setVisibility(View.GONE);
        } else {
            img_clear.setVisibility(View.VISIBLE);
        }
    }
    @Override
    public void afterTextChanged(Editable s) {

    }

    //----------------------------------------------------
}
